/*
 * User: eldad
 * Date: 19/09/2010 
 *
 * Copyright (2005) IDI. All rights reserved.
 * This software is a proprietary information of Israeli Direct Insurance.
 * Created by IntelliJ IDEA. 
 */
package com.bargate.domain.spring.expression.configuration;

import com.bargate.domain.spring.expression.Company;
import com.bargate.domain.spring.expression.Fruit;
import com.bargate.domain.spring.expression.Storm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Queue;


/**
 *
 */
@Configuration
public class ConfigurationGeneralBeans {

	@Bean
	@Qualifier("Storm")
	Storm Storm() {
		return new Storm("Rain&Wind");
	}

	@Bean
	@Qualifier("Katrina")
	Storm HurricaneKatrina() {
		Storm storm = new Storm("Hurricane - Katrina", 280.0, 202.5, true);
		storm.setOccurred(getDate(2005, 8, 23));
		return storm;
	}

	private Date getDate(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	@Bean
	@Qualifier("Blizzard")
	Storm Blizzard() {
		return new Storm("Blizzard", 150d, 100d, true);
	}

	@Bean
	@Qualifier("Hurricane")
	Storm Hurricane() {
		return new Storm("Hurricane", 250.0, true);
	}

	@Bean
	@Qualifier("Tornado")
	Storm Tornado() {
		return new Storm("Tornado", 350.0, true);
	}

	@Bean
	Fruit AnApple() {
		return new Fruit("Apple-", new Color(100, 255, 0), "light green");
	}

	@Bean
	List<Storm> Storms() {
		return createSomeStorms();
	}

	@Bean
	Queue OrderedStorms() {
//        return new PriorityQueue<Storm>(createSomeStorms());
		PriorityQueue<Storm> stormPriorityQueue = new PriorityQueue<Storm>(10);
		stormPriorityQueue.addAll(createSomeStorms());
		return stormPriorityQueue;

	}

	@Bean
	List<Integer> NumbersList() {
		return Lists.newArrayList(1, 2, 5, 6, 7, 9, 10, 12, 24, 28, 32, 36, 42, 48, 55, 66, 79, 90, 104);
	}

	@Bean
	Company CocaCola() {
		return new Company("Coca-Cola", "support@cocacola.com", "1-422-5444-321");
	}


	@Bean
	Map<Integer, Integer> NumbersMap() {
		HashMap<Integer, Integer> numbersMap = Maps.newHashMap();
		numbersMap.put(1, 1);
		numbersMap.put(2, 2);
		numbersMap.put(3, 3);
		numbersMap.put(5, 5);
		numbersMap.put(8, 8);
		numbersMap.put(13, 13);
		numbersMap.put(14, 14);
		numbersMap.put(21, 21);
		numbersMap.put(52, 52);
		numbersMap.put(66, 66);
		numbersMap.put(40, 40);
		return numbersMap;
	}

	@Bean
	Set<Integer> NumbersSet() {
		Set<Integer> orderedHashSet = Sets.newLinkedHashSet();
		orderedHashSet.add(1);
		orderedHashSet.add(2);
		orderedHashSet.add(3);
		orderedHashSet.add(5);
		orderedHashSet.add(8);
		orderedHashSet.add(13);
		orderedHashSet.add(14);
		orderedHashSet.add(21);
		orderedHashSet.add(52);
		orderedHashSet.add(66);
		orderedHashSet.add(40);
		return orderedHashSet;
	}

	@Bean
	Map<String, Storm> StormsMap() {
		List<Storm> storms = createSomeStorms();
		Map<String, Storm> mapOfStorms = Maps.newHashMap();
		for (Storm storm : storms) {
			mapOfStorms.put(storm.getType(), storm);
		}
		return mapOfStorms;
	}


	private List<Storm> createSomeStorms() {
		java.util.List<Storm> storms = Lists.newArrayList();
		storms.add(new Storm("Hail", 40.2, 10.0, true));
		storms.add(new Storm("Typhoon", 330d, 500.08, true, getDate(2001, 10, 15)));
		storms.add(new Storm("Hurricane", 250.0, true, getDate(2007, 1, 22)));
		storms.add(new Storm("Tornado", 350d, 120d, true, getDate(1991, 11, 12)));
		storms.add(new Storm("Breezer", 30d, false));
		storms.add(new Storm("Blizzard", 150d, 100d, true));
		storms.add(new Storm("Thunder", 15d, 150.5, true, getDate(2010, 2, 25)));
		storms.add(new Storm("Sand", 120d, 1150d, true, getDate(1988, 3, 4)));
		storms.add(new Storm("Gale", 50d, null, false));
		storms.add(new Storm("Snow", 60d, 25d, false));
		return storms;
	}


	public void runSomething() {
//        List<Integer> list = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
		java.util.List<Storm> list = createSomeStorms();
		ExpressionParser parser = new SpelExpressionParser();
		EvaluationContext context = new StandardEvaluationContext();
		context.setVariable("list", list);
		java.util.List<?> evenNumberList = parser.parseExpression("#list.?[#this%2==0]").getValue(context, java.util.List.class);

	}
}